package com.jarvis.hospital.util.net;


import android.util.Log;

import com.jarvis.hospital.Application;
import com.jarvis.hospital.R;
import com.jarvis.hospital.util.AddressManager;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.HttpException;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2016-10-26.
 */

public class RequestApi {
    private static String baseUrl = AddressManager.getHost();
    private static Retrofit retrofit;

    public static <T> T create(Class<T> service) {
        retrofit = new Retrofit.Builder().baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(getInterceptor())
                .build();
        return retrofit.create(service);
    }

    public static <T> T create(Class<T> service,String url) {
        retrofit = new Retrofit.Builder().baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(getInterceptor())
                .build();
        return retrofit.create(service);
    }
    private static OkHttpClient getInterceptor() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
        //新建log拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("OkHttpClient", "OkHttpMessage:" + message);
            }
        });
        loggingInterceptor.setLevel(level);
        Interceptor headerInterceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request.Builder builder = chain.request().newBuilder();
                Request requst = builder.addHeader("Content-type", "application/json").build();
                return chain.proceed(requst);
            }
        };
        //设置头
        builder.addInterceptor(headerInterceptor);
        builder.addInterceptor(loggingInterceptor);
        builder.connectTimeout(20, TimeUnit.SECONDS);
        builder.readTimeout(20, TimeUnit.SECONDS);
        builder.writeTimeout(20, TimeUnit.SECONDS);
        builder.retryOnConnectionFailure(true);
        OkHttpClient okHttpClient = builder.build();
        return okHttpClient;
    }


    public static String detailError(Throwable e) {
        if (e instanceof HttpException) {
            switch (((HttpException) e).code()) {
                case 401:
                    return Application.application.getResources().getString(R.string.net_http_401);
                case 403:
                    return Application.application.getResources().getString(R.string.net_http_403);
                case 404:
                    return Application.application.getResources().getString(R.string.net_http_404);
                case 500:
                    return Application.application.getResources().getString(R.string.net_http_500);
                case 502:
                    return Application.application.getResources().getString(R.string.net_http_502);
                default:
                    return Application.application.getResources().getString(R.string.net_http_other);
            }
        } else if (e instanceof SocketTimeoutException) {
            return Application.application.getResources().getString(R.string.net_error_timeout);
        } else if (e instanceof UnknownHostException) {
            return Application.application.getResources().getString(R.string.net_error_unknownhost);
        } else if (e instanceof InterruptedIOException) {
            return Application.application.getResources().getString(R.string.net_error_timeout);
        }
        return e.getMessage();
    }


}
